package sht.service.custom.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sht.commons.ParamUtil;
import sht.dao.custom.UserDao;
import sht.model.User;
import sht.service.custom.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	UserDao userDao;

	@Transactional
	@Override
	public void addUser(User user) {
		userDao.save(user);
	}

	@Override
	public User findUser(String userid, String password) {
		List<User> list = userDao.find("from User where userid=? and password=?",new Object[] {userid,password});
		if(list.size()>0) {
			return list.get(0);	
		}
		return null;
	}

	@Override
	public List<User> findUserByQuery(User user) {
		String sql = "from User where username=:username and password=:password ";
		Map<String,Object> mapParam = new HashMap<>();
		mapParam.put("username", user.getUsername());
		mapParam.put("password", user.getPassword());
		List<User> list = userDao.getQueryList(sql,mapParam);
		return list;
	}
}
